package zua.program.androidstudioprogram01;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    TextView username,password,register,forget_password;
    EditText input_username,input_password;
    Button login;
    Boolean isLogin = false;

    //保存函数
    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState, @NonNull PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
    }

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        input_username = findViewById(R.id.input_username);
        input_password = findViewById(R.id.input_password);
        login = findViewById(R.id.login);
        register = findViewById(R.id.register);
        forget_password = findViewById(R.id.forget_password);

        Intent intent_come = getIntent();
        input_username.setText(intent_come.getStringExtra("username"));
        //input_password.setText(intent_come.getStringExtra("password"));

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str1 = input_username.getText().toString();
                String str2 = input_password.getText().toString();
//                Intent intent1 = new Intent(MainActivity.this,QuestionActivity.class);
//                startActivity(intent1);
                if (CheckLogin(str1,str2) == true) {
                    Toast.makeText(MainActivity.this, "小可爱登陆成功啦", Toast.LENGTH_SHORT).show();
                    Intent intent1 = new Intent(MainActivity.this,PersonaInformationActivity.class);
                    intent1.putExtra("username",str1);
                    startActivity(intent1);
                }
            }
        });
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent2 = new Intent(MainActivity.this, RegisterActivity.class);
                startActivity(intent2);
            }
        });
        forget_password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str1 = input_username.getText().toString();
                Intent intent_to_ForgetPasswordActivity = new Intent(MainActivity.this,ForgetPasswordActivity.class);
                intent_to_ForgetPasswordActivity.putExtra("username",str1);
                startActivity(intent_to_ForgetPasswordActivity);
            }
        });
    }
    //之后连接数据库
    private Boolean CheckLogin(String str1,String str2) {
        if (str1.equals(getResources().getString(R.string.username_name1)) && str2.equals(getResources().getString(R.string.password_name1))) {
            isLogin = true;
        } else if (str1.equals(getResources().getString(R.string.username_name2)) && str2.equals(getResources().getString(R.string.password_name2))) {
            isLogin = true;
        } else if (str1.equals(getResources().getString(R.string.username_name3)) && (str2.equals(getResources().getString(R.string.password_name3)) || str2.equals(getResources().getString(R.string.password_other_name3)))) {
            isLogin = true;
        }
        else {
            isLogin = false;
            Toast.makeText(MainActivity.this,"小可爱，你的用户名或密码错误哦",Toast.LENGTH_LONG).show();
        }
        return isLogin;
    }

    //连接数据库Connection  getSQLConnection
//    private Boolean CheckLogin1(String str1,String str2)
//    {
//        //Connection connection = null;
//        //Boolean isLogin = false;
//        try
//        {
////            Class.forName("net.sourceforge.jtds.jdbc.Driver");
//            Class.forName("com.mysql.jdbc.Driver");
//            String url = "jdbc:mysql://localhost:3306/myapplication?useUnicode=true&characterEncoding=utf8&useSSL=true";
//            Connection connection = DriverManager.getConnection(url, "root", "root");
//            Statement statement = connection.createStatement();
//            String sql = "select password from demo01 where username = "+"'"+str1+"'";
//            ResultSet resultSet = statement.executeQuery(sql);//返回的结果集，结果集封装了我们全部的查询结果
//
//            String password = null;
//            while (resultSet.next()) {
//                password = (String) resultSet.getObject("password");
//            }
//            if (str2.equals(password)) {
//                isLogin = true;
//            } else {
//                isLogin = false;
//                Toast.makeText(MainActivity.this,"用户名或密码错误",Toast.LENGTH_LONG).show();
//            }
//            resultSet.close();
//            statement.close();
//            connection.close();
//
//        } catch (ClassNotFoundException e)
//        {
//            e.printStackTrace();
//        } catch (SQLException e)
//        {
//            e.printStackTrace();
//        }
//        return isLogin;
//    }
}